package cn.usian.service.employeeserviceimpl;

import cn.usian.common.R;
import cn.usian.dao.CategoryMapper;
import cn.usian.dao.DishMapper;
import cn.usian.dao.SetmealMapper;
import cn.usian.entity.Category;
import cn.usian.entity.Dish;
import cn.usian.entity.Setmeal;
import cn.usian.exception.CustomerException;
import cn.usian.exception.NameException;
import cn.usian.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;
    @Override
    public Page<Category> findPage(Page<Category> page1) {
        LambdaQueryWrapper<Category> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        return categoryMapper.selectPage(page1,queryWrapper);
    }

    @Override
    public void addCategory(Category category) {
        //判断分类明是否存在
        if(findByName(category.getName())!=null){
            throw new NameException(category.getName()+"已存在");
        }
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.insert(category);
    }

    @Override
    public void delCategory(Long id) {
        //根据id查询dish和setmeal表是否有关联数据
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Dish::getCategoryId,id);
        Dish dish=dishMapper.selectOne(queryWrapper1);
        if(dish !=null){
           throw new CustomerException("关联其他菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> queryWrapper2= new LambdaQueryWrapper<>();
        queryWrapper2.eq(Setmeal::getCategoryId,id);
        List<Setmeal>setmealList=setmealMapper.selectList(queryWrapper2);
        if(setmealList.isEmpty()){
          throw new CustomerException("关联其他套餐，不能删除");
        }
        categoryMapper.deleteById(id);
    }

    @Override
    public void updateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        LambdaUpdateWrapper<Category> updateWrapper = new LambdaUpdateWrapper<>();
        if(category.getName()!=null && !category.getName().equals("")){
            updateWrapper.set(Category::getName,category.getName());
        }
        if(category.getSort()!=null){
            updateWrapper.set(Category::getSort,category.getSort());
        }
        updateWrapper.eq(Category::getId,category.getId());
        categoryMapper.update(category,updateWrapper);
    }

    @Override
    public R findById(Long id) {
        return R.success(categoryMapper.selectById(id));
    }

    @Override
    public Category findByName(String name) {
        LambdaQueryWrapper<Category> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName,name);
        return categoryMapper.selectOne(queryWrapper);
    }

    @Override
    public R findList(Integer type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        if(type !=null){
            queryWrapper.eq(Category::getType,type);
        }
        return R.success(categoryMapper.selectList(queryWrapper));
    }

    @Override
    public List<Category> findAll() {
        return categoryMapper.selectList(null);
    }
}
